package org.tio.sitexxx.service.service.qj;

import cn.hutool.http.HttpRequest;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.model.main.Wardrobe;
import org.tio.sitexxx.service.vo.Const;

import java.sql.SQLException;
import java.util.*;

/**
 * 客服控制是否支付以及改变支付状态
 */
public class SkipService {
    private static Logger log = LoggerFactory.getLogger(SkipService.class);
    public static final SkipService me = new SkipService();

    public SkipService() {

    }

    /**
     * 返回consultant_member_valid表里的记录
     *
     * @param mid
     * @return select * from consultant_member_valid where  member_id=#para(0)
     */
    public List<Map<String, Object>> findIsValid(int mid) {
        List<Map<String, Object>> resultlist = new ArrayList<>();
        String sqlid = "skip.findIsValid";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, mid);
        List<Record> record = Db.use(db).find(sqlPara);
        if (record != null) {
            for (Record record1 : record) {
                resultlist.add(JSON.parseObject(JSON.toJSONString(record1.getColumns())));
            }
        }
        return resultlist;
    }

    /**
     * 返回当前咨询member的id
     *
     * @param openid
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, Integer> findMId(String openid) {
        Map<String, Integer> resultmap = new HashMap<>(3);
        String sqlid = "skip.findMId";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, openid);
        Record record = Db.use(db).findFirst(sqlPara);
        if (record != null) {
            resultmap = JSON.parseObject(JSON.toJSONString(record.getColumns()), Map.class);
        }
        return resultmap;
    }

    /**
     * 判断是否支付成功
     *
     * @param orderid
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, Boolean> select_is_paid(String orderid) {
        Map<String, Boolean> resultmap = new HashMap<>(3);
        String sqlid = "skip.select_is_paid";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, orderid);
        Record record = Db.use(db).findFirst(sqlPara);
        if (record != null) {
            resultmap = JSON.parseObject(JSON.toJSONString(record.getColumns()), Map.class);
        }
        return resultmap;
    }

    /**
     * 返回expired 1 为有效 0无效 2已失效
     *
     * @param mId
     * @param cId
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, Integer> isSaveValid(int mId, int cId) {
        Map<String, Integer> resultmap = null;
        String sqlid = "order.isSaveValid";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, mId, cId);
        System.out.println("isvalid:" + sqlPara.getSql() + " " + mId + " " + cId);
        Record record = Db.use(db).findFirst(sqlPara);
        if (record != null) {
            resultmap = JSON.parseObject(JSON.toJSONString(record.getColumns()), Map.class);
        }
        return resultmap;
    }

    /**
     * 支付成功改变consultant_member_valid 表expired的状态
     *
     * @param expired
     * @param member_id
     * @param consultant_id
     * @return
     */
    public Boolean updateValidTime(int expired, int member_id, int consultant_id) {
        String sqlid = "order.addValidTime";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, expired, member_id, consultant_id);
        System.out.println("sqlupdateValidTime:" + sqlPara.getSql());
        Db.use(db).update(sqlPara.getSql(), expired, member_id, consultant_id);
        return true;

    }


    /**
     * 咨询的客服结束当前咨询 将expired置为2
     *
     * @param cid
     * @param mid
     * @return
     */
    public boolean timeend(int cid, int mid) {
        System.out.println("cid:" + cid + "mid:" + mid);
        String sqlid = "skip.timeend";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, cid, mid);
        System.out.println("sql:" + sqlPara.getSql());
        Db.use(db).update(sqlPara.getSql(), cid, mid);
        return true;
    }

    /**
     * 返回历史消息记录每次20条
     *
     * @param uid
     * @param touid
     * @param pageNum
     * @return
     */
    public List<Map<String, Object>> chat_record(Integer uid, Integer touid, Integer pageNum) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String twouid = Math.max(uid, touid) + "_" + Math.min(uid, touid);
        String sqlid = "skip.chat_record";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, twouid, pageNum*20);//每页请求二十条数据
        List<Record> record = Db.use(db).find(sqlPara);
        if (record != null) {
            for (Record record1 : record) {
                resultList.add(record1.getColumns());
            }
        }
        return resultList;
    }

    /**
     * 用户撤回消息  更新chat_record_p2p status 为2
     *
     * @param mid
     * @return
     */
    public boolean update_record(Integer mid) {

        String sqlid = "skip.update_record";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid, mid);
        System.out.println("sql:" + sqlPara.getSql());
        Db.use(db).update(sqlPara.getSql(), mid);
        return true;
    }

    /**
     * temp  新旧数据库过渡
     *
     * @return
     */
    public boolean member_change_to_user() {
        List<Member> list = new ArrayList<>(12);

//        String sqlid = "skip.member_to_user1";
        String db = Const.Db.TIO_SITE_MAIN;
//        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
//        System.out.println("sql:" + sqlPara.getSql());
        List<Record> records = Db.use(db).find("select id, openid,nickname,avatar,name from member");

        if (records != null) {
            Db.tx(new IAtom() {
                @Override
                public boolean run() throws SQLException {
                    records.forEach((records1) -> {
                        Map<String, Object> temp = records1.getColumns();
                        Integer id = Integer.valueOf(temp.get("id").toString());
                        String openid = String.valueOf(temp.get("openid"));
                        String nickname = String.valueOf(temp.get("nickname"));
                        String name=String.valueOf(temp.get("name"));
                        String username=!name.equals("null")?name:nickname;
                        Date date = new Date();
                        String avatar = String.valueOf(temp.get("avatar"));
                        Record user = new Record().set("id", id).set("loginname", openid).set("nick", username)
                                .set("status", 1).set("avatar", avatar).set("pwd", "").set("ipid", 0);
                        Db.use(Const.Db.TIO_SITE_MAIN).save("user", user);

                    });
                    return true;
                }
            });
            System.out.println("ok");
            return true;
        }
        return false;
        /*String sqlid = "skip.member_to_user";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        System.out.println("sql:"+sqlPara.getSql());
        List<Record> records=Db.use(db).find(sqlPara.getSql());
        if(records!=null){
            Db.tx(new IAtom() {
                @Override
                public boolean run() throws SQLException {
                    records.forEach((records1)->{
                        Map<String,Object> temp= records1.getColumns();
                        Integer id=Integer.valueOf(temp.get("id").toString());
                        String openid=String.valueOf(temp.get("openid"));
                        String nickname=String.valueOf(temp.get("nickname"));
                        Date date=new Date();
                        String avatar=String.valueOf(temp.get("avatar"));
                        Record user=new Record().set("id",id).set("loginname",openid).set("nick",nickname)
                                .set("status",1).set("avatar",avatar).set("pwd","").set("ipid",0);
                        Db.use(Const.Db.TIO_SITE_MAIN).save("user",user);

                    });
                    return true;
                }
            });
            return true;
        }
        return false ;*/

        /*Record user=new Record().set("id",50000).set("loginname","ot5ov5cTb-cTXN4fA84axOdPSfjA").set("nick","谭林测试客服")
                .set("status",1).set("avatar","https://wx.qlogo.cn/mmopen/vi_32/ibgbXnszlruMdsVHKVnhNl670N58q6pwTTlGRficx7AeDt7CDerQHblm3xkf39SlEOft78OjjmBN23vPyHcFA5kg/132").set("pwd","").set("ipid",0);
        Db.use(Const.Db.TIO_SITE_MAIN).save("user",user);
        return true;*/
    }

    /**
     * 解决member表中nickname为空的问题
     */
    public void member_nickname() {
        String sql = "select * from member";
        List<Member> list = Member.dao.find(sql);
        Db.tx(new IAtom() {
            @Override
            public boolean run() throws SQLException {
                list.forEach(member -> {
                    if (member.getName() != null && member.getNickname() == null) {
                        member.setNickname(member.getName());
                        member.update();
                    }
                });
                return true;
            }
        });
        System.out.println("ok");
    }

   /* public static void main(String[] args) {
        me.member_change_to_user();
    }*/
}
